
var chatTemp=$(
'<div id="chatbox">'
	+'<div id="chatview" class="p1" style="display: block;">'    	
		+'<div id="profile" class="animate">'
			+'<div id="close">'
				+'<div class="cy s1 s2 s3"></div>'
				+'<div class="cx s1 s2 s3"></div>'
			+'</div>'
			+'<p class="animate">Miro Badev</p>'
			+'<span>miro@badev@gmail.com</span>'
			+'<div class="inputPrompt">对方正在输入...</div>'
		+'</div>'
		+'<div id="chat-messages" class="animate">'
			+'<label>星期二</label>'
			+'<div id="sendmessage">'
			+'<input type="text" id="content" value="">'
			+'<input type="hidden" value="">'
			+'<input type="button" value="send" onclick="send(this)">'
		+'</div>'
	+'</div>'        
+'</div>'
+'<img src="images/1.jpg" class="floatingImg" style="top: 20px; width: 68px; left: 108px;"></div>');

var newsTemp=null;

$(function(){
	
	newsTemp=$('<div class="message"><img src="'+picture+'"/><div class="bubble"></div></div>');
	
	//加载页面创建连接
	$(window).load(function(){
		//自己的IP地址
		var ws=chatWs.createConnection("192.168.1.5:8090");

		console.log(ws)
		//设置webSocket对象的回调函数，接收服务器的消息
		ws.onmessage=function(event){
			// console.log(event)  后台socket的json数据传到event.data中。
			var message=JSON.parse(event.data);
			
			//1发消息  2上线  3下线  4回馈  5初始化  6输入提示
			if(message.type===1){
				chatInit(2,message,chatWs.formatDate(message.messageDate));
			}else if(message.type===2){
				$("#chatbox").find("#profile").find("span").text(message.content);
			}else if(message.type===4){
				if(message.unreceivedMessage==null){
					$("#chatbox").find(".bubble").find("[type=hidden]").each(function(){
						if($(this).attr("value")==message.messageDate){
							$(this).parent().find(".isRead").remove();
						}
					});
				}else{
					$(message.unreceivedMessage).each(function(){
						var temp=this;
						$("#chatbox").find(".bubble").find("[type=hidden]").each(function(){
							if($(this).attr("value")==temp.messageDate){
								$(this).parent().find(".isRead").remove();
							}
						});
					});
				}
			}else if(message.type===5){
				$(message.unreadMessage).each(function(){
					chatInit(1,this,chatWs.formatDate(this.messageDate));
				});
				$(message.unreceivedMessage).each(function(){
					chatInit(2,this,chatWs.formatDate(this.messageDate));
				});
				if(message.unreceivedMessage!=null){
					chatWs.feedback(message);
				}
			}else if(message.type===6){
				$("[type=hidden]").each(function(){
					if($(this).val()==message.sender.userId){
						var temp=$(this).parents("#chatview").find("#profile").find("div");
						temp.toggleClass("inputPrompt");
					}
				});
			}
		};
	});
	
	//关闭或刷新页面断开连接
	$(window).bind('beforeunload', function(event) {
		chatWs.userOffline();
	});
	
});


/*
 * 添加消息框
 * flag:1-自己发送的消息；2-接收的消息；
 * message:消息对象
 * date:消息发送日期，仅在页面显示，与后台无关
 */
function chatInit(flag,message,date){
	var temp=newsTemp.clone();
	if(flag==1){
		temp.attr("class","message right");
		temp.css("position","static");
		temp.find(".bubble").html(message.content+'<input type="hidden" value="'+message.messageDate+'"><span>'+date+'<span class="isRead">未读</span></span>');
	}else{
		var picUrl=$("#sendmessage").parents(".user_card").find(".avatar-pic").find("a").find("img").attr("src");
		temp.find("img").attr("src",picUrl);
		temp.find(".bubble").html(message.content+'<span class="dateStyle">'+date+'</span>');
	}
	$("#sendmessage").before(temp);
}

/*
 * 添加对话窗口的关闭事件
 * obj:指向chatbox结点
 */
function addListener(obj){
	obj.find("#close").click(function(){
		$($(this).parents(".user_card").find(".button1")[1]).text("私 信");
		$(this).parents("#chatbox").remove();
	});
	
	var content=$(obj).find("#content");
	
	content.blur(function(){
		var state=$(this).parents("#chatview").find("#profile").find("span").text();
		if(state==="在线"){
			chatWs.inputPrompt($(this).next().val());
		}
	});
	
	content.focus(function(){
		var state=$(this).parents("#chatview").find("#profile").find("span").text();
		if(state==="在线"){
			chatWs.inputPrompt($(this).next().val());
		}
	});
}

/*
 * 打开对话窗口
 * obj:指向私信结点
 * userName:私信对象的userName
 * userId:私信对象的userId
 * userPicture:私信对象的userPicture
 */
function chat(obj,userName,userId,userPicture){
	//上线通知
	chatWs.userOnline(userId);
	
	//打开对话窗口
	var temp=$(obj).parents(".user_card");
	var msgBox=temp.find(".msgBox");
	if(msgBox.prop("class")!='msgBox'){
		var target=chatTemp.clone(false);
		target.find(".animate").find("p").text(userName);
		target.find(".animate").find("span").text("对方不在线");
		target.find("img").attr("src",userPicture);
		target.find("#chat-messages").find("label").text(chatWs.getWeek());
		target.find("#chat-messages").find("[type=hidden]").attr("value",userId);
		addListener(target);
		temp.append(target);
		$(obj).text("关闭私信");
	}else{
		msgBox.remove();
		$(obj).text("私 信");
	}
	
	//获取未发送消息和未接收消息
	chatWs.initNews(userId);
}

/*
 * 发送消息
 * obj:指向对话窗口的send按钮
 */
function send(obj){
	$(obj).prev().prev().attr("value","");
	var message=chatWs.sendNews($("#content").val(),$(obj).prev().attr("value"))
	chatInit(1,message,chatWs.formatDate());
}